package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.qurey.StrategyCatalogQuery;
import cn.wolfcode.wolf2w.repository.StrategyCatalogRepository;
import cn.wolfcode.wolf2w.repository.StrategyRepository;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.util.DBHepler;
import cn.wolfcode.wolf2w.vo.CatalogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
//@Transactional mongodb事务必须复制集方式

public class StrategyCatalogServiceImpl implements IStrategyCatalogService {

    @Autowired
    private StrategyCatalogRepository repository;

    @Autowired
    private MongoTemplate template;

    @Autowired
    private IStrategyService strategyService;


    @Override
    public void save(StrategyCatalog strategyCatalog) {
        strategyCatalog.setId(null);
        repository.save(strategyCatalog);
    }

    @Override
    public void update(StrategyCatalog strategyCatalog) {
        repository.save(strategyCatalog);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public StrategyCatalog get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<StrategyCatalog> list() {
        return repository.findAll();
    }

    @Override
    public Page<StrategyCatalog> query(StrategyCatalogQuery qo) {
        Query query = new Query();
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.Direction.ASC, "_id");
        return DBHepler.query(template,pageable,query, StrategyCatalog.class,qo);
    }

    @Override
    public void saveOrUpdate(StrategyCatalog strategyCatalog) {
        if (strategyCatalog.getId()!=null){
            this.update(strategyCatalog);
        }else {
            this.save(strategyCatalog);
        }

    }

    @Override
    public List<CatalogVO> groupList() {
        List<CatalogVO> list =new ArrayList<>();
        //查询mongdb数据
        //聚合查询

        TypedAggregation<StrategyCatalog> agg = Aggregation.newAggregation(StrategyCatalog.class,

                Aggregation.group("destName").

                        push("name").as("names").

                        push("id").as("ids")

        );

        AggregationResults<Map> result = template.aggregate(agg,Map.class);
        //将结果转化成list<map>   map描述一行数据
        List<Map> datas = result.getMappedResults();

        //封装页面共享数据
        for (Map data : datas) {

            CatalogVO vo = new CatalogVO();
            //id
            vo.setDestName(data.get("_id").toString());
            //攻略分类的名称集合
            List<Object> names = (List<Object>) data.get("names");
            //攻略分类的ids集合
            List<Object> ids = (List<Object>) data.get("ids");
            //攻略分类对象
            List<StrategyCatalog> catalogList = new ArrayList<>();

            for(int i = 0;i < names.size(); i++){

                StrategyCatalog sc = new StrategyCatalog();

                String name = names.get(i).toString();

                String id = ids.get(i).toString();

                sc.setId(id);

                sc.setName(name);

                catalogList.add(sc);

            }

            vo.setCatalogList(catalogList);

            list.add(vo);

        }

        return list;
    }

    @Override
    public List<StrategyCatalog> queryCatalogsByDestId(String destId) {
        //攻略分类的集合
        List<StrategyCatalog> catalogs = repository.findByDestId(destId);

        //攻略分类集合关联明细集合
        for (StrategyCatalog catalog : catalogs) {
           List<Strategy> sts= strategyService.queryByCatalogId(catalog.getId());
           catalog.setStrategies(sts);
        }
        return catalogs;
    }


}
